package com.cskaoyan.javase.homework.day18;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * @description: 统计各种字符
 * @author: songtao@cskaoyan.onaliyun.com
 **/

/*1.  从磁盘上读取一个文本文件（如某个java源代码），
分别统计出文件中英文字母、空格、数字字符的个数。(注意文本文件中的数字指的是数字字符！！！)*/
public class ReaderDemo {
    public static void main(String[] args) {
        Reader reader = null;
        int digitalNum = 0;
        int charsNum = 0;
        int spaceNum = 0;
        try {
            reader = new FileReader("homework\\src\\io\\homework2\\Homework2.java");
            char[] chars = new char[1024];
            int len;
            while ((len = reader.read(chars)) != -1) {
                for (int i = 0; i < len; i++) {
                    if (chars[i] >= '0' && chars[i] <= '9') {
                        digitalNum++;
                    } else if ((chars[i] >= 'a' && chars[i] <= 'z') ||
                            (chars[i] >= 'A' && chars[i] <= 'Z')) {
                        charsNum++;
                    } else if (chars[i] == ' ') {
                        spaceNum++;
                    }
                }
            }
            System.out.println("digitalNum: " + digitalNum);
            System.out.println("charsNum: " + charsNum);
            System.out.println("spaceNum: " + spaceNum);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {

                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
